#!/usr/bin/perl

use strict;
use modules::encode;
package csdeop;

sub csdeop {
    my ($dbh,$remote,$input,$csnum) = @_;
    my $sth; my $auth;
    my $tmp; #this holds the person's num
    if ($$input[5] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :DEOP <channel> <nick>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	if (! ($auth=$sth->fetchrow_array) ) {
	    print $remote "$csnum O $$input[0] :you have to identify yourself with nickserv before using this command.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT rnick FROM users WHERE channel = '$$input[4]' AND rnick = '$auth' AND level >= 150;");
	    $sth->execute;
	    if (!$sth->fetchrow_array) {
		print $remote "$csnum O $$input[0] :Access denied.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT num FROM ninfo WHERE nick = '$$input[5]';");
		$sth->execute;
		$tmp = $sth->fetchrow_array;
		$sth=$dbh->prepare("SELECT num FROM onchan WHERE channel = '$$input[4]' AND num = '$tmp' AND code = '".encode::encode($tmp)."' AND isop = '1';");
		$sth->execute;
		if (!$sth->fetchrow_array) {
		    print $remote "$csnum O $$input[0] :$$input[5] is not opped in $$input[4]\n";
		}
		else {
		    print $remote "$csnum M $$input[4] -o $tmp\n";
		    print $remote "$csnum O $$input[0] :Deopped $$input[5] in $$input[4]\n";
		}
	    }
	}
    }

}
1;
